package com.wwj.admin.deliver.controller;

import com.wwj.admin.BaseCommon;
import com.wwj.core.wawa.entity.DeliverRecord;
import com.wwj.core.wawa.entity.DeliverRecordDetail;
import com.wwj.core.wawa.service.IDeliverRecordService;
import com.wwj.framework.utils.Page2EasyUiData;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Auser on 2017/11/24.
 */
@Controller
@RequestMapping("/deliver")
public class DeliverRecordController extends BaseCommon {

    private final String CHECK = "/admin/deliver/audit";
    private final String AWAIT = "/admin/deliver/await";
    private final String SHIPPED = "/admin/deliver/shipped";
    private final String DENIED = "/admin/deliver/denied";

    @Resource
    private IDeliverRecordService deliverRecordService;

    @RequestMapping(value = {"/audit", "/audit/", "/audit/index"})
    public String check() throws Exception {
        return CHECK;
    }

    @RequestMapping(value = {"/await", "/await/", "/await/index"})
    public String await() throws Exception {
        return AWAIT;
    }

    @RequestMapping(value = {"/shipped", "/shipped/", "/shipped/index"})
    public String shipped() throws Exception {
        return SHIPPED;
    }

    @RequestMapping(value = {"/denied", "/denied/", "/denied/index"})
    public String denied() throws Exception {
        return DENIED;
    }


    @RequestMapping("/listData.json")
    @ResponseBody
    public ReturnData list(String applyStartDate, String applyStopDate,
                           String deliverStartDate, String deliverStopDate,
                           String auditStartDate, String auditStopDate,
                           String search, Integer status, int offset, int limit) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        if (null != applyStartDate && !"".equals(applyStartDate)) params.put("applyStartDate", applyStartDate);
        if (null != applyStopDate && !"".equals(applyStopDate)) params.put("applyStopDate", applyStopDate);
        if (null != auditStartDate && !"".equals(auditStartDate)) params.put("auditStartDate", auditStartDate);
        if (null != auditStopDate && !"".equals(auditStopDate)) params.put("auditStopDate", auditStopDate);
        if (null != deliverStartDate && !"".equals(deliverStartDate)) params.put("deliverStartDate", deliverStartDate);
        if (null != deliverStopDate && !"".equals(deliverStopDate)) params.put("deliverStopDate", deliverStopDate);
        if (null != search && !"".equals(search)) params.put("search", search);
        if (null != status && !"".equals(status)) params.put("status", status);
        return new ReturnData().success(Page2EasyUiData.format(deliverRecordService.findByPage(params, offset, limit)));
    }


    @RequestMapping("/audit_opt")
    @ResponseBody
    public ReturnData audit(DeliverRecord deliverRecord) throws Exception {
        if (null == deliverRecord) return new ReturnData().failure("参数不能为空");
        if (null == deliverRecord.getId()) return new ReturnData().failure("参数ID不能为空");
        if (null == deliverRecord.getStatus()) return new ReturnData().failure("状态参数异常");
        deliverRecord.setAuditTime(new Date());
        deliverRecordService.updateDeliverRecord(deliverRecord);
        return new ReturnData().success("审核完成");
    }

    @RequestMapping("/shipments_opt")
    @ResponseBody
    public ReturnData check(DeliverRecord deliverRecord) throws Exception {
        if (null == deliverRecord) return new ReturnData().failure("参数不能为空");
        if (null == deliverRecord.getId()) return new ReturnData().failure("参数ID不能为空");
        deliverRecord.setStatus(DeliverRecord.STATUS_SENDED);
        deliverRecord.setDeliverTime(new Date());
        deliverRecordService.updateDeliverRecord(deliverRecord);
        return new ReturnData().success("发货完成");
    }


    @RequestMapping("/{id}/detail/listData.json")
    @ResponseBody
    public ReturnData list(@PathVariable("id") Integer id) throws Exception {
        List<DeliverRecordDetail> list = deliverRecordService.findDetail(id);
        return new ReturnData().success(Page2EasyUiData.format(list));
    }
}
